﻿#include "myNotePad.h"

#include <QListWidget>
#include <QListWidgetItem>
#include <QShortcut>
#include "CustomListWidgetItem.h"

myNotePad::myNotePad(QWidget *parent)
	: QMainWindow(parent),NotePage()
{
	ui.setupUi(this);
	QFont font;
	font.setPointSize(font_size);
	mainText->setFont(font);
	//this->view_mode = NotePadConfig.view_mode_default;
	myNotePad::loadWidget();

	//other implement...
}

myNotePad::~myNotePad()
{}

void myNotePad::loadWidget()
{
	//textEdit = new myNotePad(this);
	//setCentralWidget(textEdit);//设置为中心控件
	QVBoxLayout* layout = new QVBoxLayout(ui.centralWidget);
	layout->setContentsMargins(0, 0, 0, 0); // 设置layout边距为0,填充满主窗口

	//设置光标
	//const iQTextCursor& mycursor = ;//当前窗口的光标实体
	//mainText->setFocus();
	mainText->setTextCursor(this->getiTextCursor());
	//文本框控件设置
	mainText->setObjectName("mainText");
	mainText->setStyleSheet("border: none;");
	layout->addWidget(mainText);
	


	

	QPushButton* button = new QPushButton("markdown转换按键", this);
	button->setStyleSheet("QPushButton{ color: red }");
	button->setObjectName("button");
	statusBar()->addWidget(button);

	connect(
		ui.statusBar->findChild<QPushButton*>("button", Qt::FindDirectChildrenOnly),
		&QPushButton::clicked,
		this,
		&myNotePad::soureCodeModeActionSlot
	);
	QDockWidget* DocW = new QDockWidget("资源管理器", this);
	DocW->setObjectName("DocW");
	addDockWidget(Qt::LeftDockWidgetArea, DocW);
	QTabWidget* TabW = new QTabWidget(DocW);
	TabW->setObjectName("TabW");
	DocW->setWidget(TabW); // 设置QTabWidget为QDockWidget的主要窗口部件
	//创建标签
		//QWidget* FileTab = new QWidget(TabW);
	QWidget* OutlineTab = new QWidget(TabW);
	//设置标签
		//TabW->addTab(FileTab, "文件");
	TabW->addTab(OutlineTab, "大纲");
	//实例化显示"大纲"的树状图并初始化数值
	
	QListWidget* OutlineList = new QListWidget(OutlineTab);

	OutlineList->setSortingEnabled(true);//启用自动排序
	OutlineList->sortItems(); // 按照行号排序

	//布局管理器
	QVBoxLayout* Tablayout = new QVBoxLayout(OutlineTab); // 为OutlineTab设置布局管理器
	Tablayout->addWidget(OutlineList); // 添加QTreeWidget到布局管理器中

	/*--------------------------------------------------------------------------------------------------------*/

	connect(
		mainText,
		&QTextEdit::textChanged,
		this,
		&myNotePad::chargeText
	);
	connect(
		mainText,
		&QTextEdit::cursorPositionChanged,
		this,
		&myNotePad::insertdisplay
	);

	//新建文件快捷键
	ui.newAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
	connect(
		ui.newAction,
		SIGNAL(triggered(bool)),
		this,
		SLOT(newActionSlot)
	);
	connect(
		ui.newAction,
		&QAction::triggered,
		this,
		&myNotePad::newActionSlot
	);

	//打开文件快捷键
	ui.openAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
	connect(
		ui.openAction,
		SIGNAL(triggered(bool)),
		this,
		SLOT(openActionSlot)
	);
	connect(
		ui.openAction,
		&QAction::triggered,
		this,
		&myNotePad::openActionSlot
	);

	//保存文件快捷键
	ui.saveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
	connect(
		ui.saveAction,
		SIGNAL(triggered(bool)),
		this,
		SLOT(saveActionSlot)
	);
	connect(
		ui.saveAction,
		&QAction::triggered,
		this,
		&myNotePad::saveActionSlot
	);
	
	//另存为文件快捷键
	ui.saveAsAction->setShortcut(QKeySequence::fromString("Ctrl+Shift+S"));
	connect(
		ui.saveAsAction,
		SIGNAL(triggered(bool)),
		this,
		SLOT(saveAsActionSlot)
	);
	connect(
		ui.saveAsAction,
		&QAction::triggered,
		this,
		&myNotePad::saveAsActionSlot
	);


	//关闭文件快捷键
	//ui.closeAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
	connect(
		ui.closeAction,
		SIGNAL(triggered(bool)),
		this,
		SLOT(closeActionSlot)
	);
	connect(
		ui.closeAction,
		&QAction::triggered,
		this,
		&myNotePad::closeActionSlot
	);
	//-------------------------------------------------------------编辑
	
	connect(
		ui.undoAction,
		&QAction::triggered,
		this,
		&myNotePad::undoActionSlot
	);

	connect(
		ui.cutAction,
		&QAction::triggered,
		this,
		&myNotePad::cutActionSlot
	);
	connect(
		ui.copyAction,
		&QAction::triggered,
		this,
		&myNotePad::copyActionSlot
	);
	connect(
		ui.pasteAction,
		&QAction::triggered,
		this,
		&myNotePad::pasteActionSlot
	);
	connect(
		ui.copyAsPlainTextAction,
		&QAction::triggered,
		this,
		&myNotePad::copyAsPlainTextActionSlot
	);
	connect(
		ui.copyAsMarkdown,
		&QAction::triggered,
		this,
		&myNotePad::copyAsMarkdownSlot
	);
	connect(
		ui.copyAsHTML,
		&QAction::triggered,
		this,
		&myNotePad::copyAsHTMLSlot
	);
	connect(
		ui.emojiAndSymbolsAction,
		&QAction::triggered,
		this,
		&myNotePad::emojiAndSymbolsActionSlot
	);
	//---------------------------段落----------------------------------
	//快捷键设置(加急未整理版)
	QShortcut* h1 = new QShortcut(this);
	h1->setKey(QKeySequence("Ctrl+1"));  // 使用自定义的键盘组合
	connect(
		h1,
		&QShortcut::activated,
		this,
		[=]() {
			this->headingActionSlot(1);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);

		}
	);
	QShortcut* h2 = new QShortcut(this);
	h2->setKey(QKeySequence("Ctrl+2"));  // 使用自定义的键盘组合
	connect(
		h2,
		&QShortcut::activated,
		this,
		[=]() {
			this->headingActionSlot(2);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "  " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);

		}
	);
	QShortcut* h3 = new QShortcut(this);
	h3->setKey(QKeySequence("Ctrl+3"));  // 使用自定义的键盘组合
	connect(
		h3,
		&QShortcut::activated,
		this,
		[=]() {
			this->headingActionSlot(3);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "    " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);

		}
	);
	QShortcut* h4 = new QShortcut(this);
	h4->setKey(QKeySequence("Ctrl+4"));  // 使用自定义的键盘组合
	connect(
		h4,
		&QShortcut::activated,
		this,
		[=]() {
			this->headingActionSlot(4);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "      " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);

		}
	);
	QShortcut* h5 = new QShortcut(this);
	h5->setKey(QKeySequence("Ctrl+5"));  // 使用自定义的键盘组合
	connect(
		h5,
		&QShortcut::activated,
		this,
		[=]() {
			this->headingActionSlot(5);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "        " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);

		}
	);
	QShortcut* h6 = new QShortcut(this);
	h6->setKey(QKeySequence("Ctrl+6"));  // 使用自定义的键盘组合
	connect(
		h6,
		&QShortcut::activated,
		this,
		[=]() {
			this->headingActionSlot(6);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "          " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);

		}
	);
	
	//标题功能
	connect(
		ui.heading1Action,
		&QAction::triggered,
		this,
		[=]() {
			this->headingActionSlot(1);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			CustomListWidgetItem* temp = new CustomListWidgetItem(header,line);
			OutlineList->addItem(temp);

		}
	);
	connect(
		ui.heading2Action,
		&QAction::triggered,
		this,
		[=](){
			this->headingActionSlot(2);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "  " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);
		}
	);
	connect(
		ui.heading3Action,
		&QAction::triggered,
		this,
		[=]() {
			this->headingActionSlot(3);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "    " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);
		}
	);
	connect(
		ui.heading4Action,
		&QAction::triggered,
		this,
		[=]() {
			this->headingActionSlot(4);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "      " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);
		}
	);
	connect(
		ui.heading5Action,
		&QAction::triggered,
		this,
		[=]() {
			this->headingActionSlot(5);
			int line = mainText->textCursor().blockNumber();
			QString header = mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "        " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);
		}
	);
	connect(
		ui.heading6Action,
		&QAction::triggered,
		this,
		[=]() {
			this->headingActionSlot(6);
			int line = mainText->textCursor().blockNumber();
			QString header =  mainText->document()->findBlockByNumber(line).text();
			regex(header, "^#+");
			header = "          " + header;
			CustomListWidgetItem* temp = new CustomListWidgetItem(header, line);
			OutlineList->addItem(temp);
		}
	);
	//跳转功能
	connect(
		OutlineList,
		&QListWidget::itemClicked,//存在唯一有参 QListWidgetItem*
		this,
		[=](QListWidgetItem* item) {
			CustomListWidgetItem* customItem = dynamic_cast<CustomListWidgetItem*>(item);
			int lineNumber = customItem->lineNumber();// 要跳转的行号

			QTextCursor cursor = mainText->textCursor(); // 获取当前的文本光标
			int position = mainText->document()->findBlockByLineNumber(lineNumber).position(); // 获取指定行的位置
			cursor.setPosition(position); // 设置光标位置

			mainText->setTextCursor(cursor); // 设置文本编辑器的文本光标为新的位置
			mainText->ensureCursorVisible(); // 确保光标可见
		}
	);

	connect(
		ui.increaseHeadingLevelAction,
		&QAction::triggered,
		this,
		&myNotePad::increaseHeadingLevelActionSlot
	);
	connect(
		ui.decreaseHeadingLevelAction,
		&QAction::triggered,
		this,
		&myNotePad::decreaseHeadingLevelActionSlot
	);
	connect(
		ui.tableAction,
		&QAction::triggered,
		this,
		&myNotePad::tableActionSlot
	);
	connect(
		ui.codeFencesAction,
		&QAction::triggered,
		this,
		&myNotePad::codeFencesActionSlot
	);
	connect(
		ui.mathBlockAction,
		&QAction::triggered,
		this,
		&myNotePad::mathBlockActionSlot
	);
	connect(
		ui.quoteAction,
		&QAction::triggered,
		this,
		&myNotePad::quoteActionSlot
	);
	//------------------------格式-------------------------------------

	connect(
		ui.strongAction,
		&QAction::triggered,
		this,
		&myNotePad::strongActionSlot
	);
	connect(        //斜体
		ui.emphasisAction,
		&QAction::triggered,
		this,
		&myNotePad::emphasisActionSlot
	);

	connect(        //下划线
		ui.underLineAction,
		&QAction::triggered,
		this,
		&myNotePad::underLineActionSlot
	);
	connect(
		ui.codeAction,
		&QAction::triggered,
		this,
		&myNotePad::codeActionSlot
	);
	connect(
		ui.strikeAction,
		&QAction::triggered,
		this,
		&myNotePad::strikeActionSlot
	);
	connect(
		ui.hyperlinkAction,
		&QAction::triggered,
		this,
		&myNotePad::hyperlinkActionSlot
	);
	connect(
		ui.clearFormatAction,
		&QAction::triggered,
		this,
		&myNotePad::clearFormatActionSlot
	);
	//-------------------------------------------------------------
	connect(
		ui.toggleSidebarAction,
		&QAction::triggered,
		this,
		&myNotePad::toggleSidebarActionSlot
	);
	connect(
		ui.outlineAction,
		&QAction::triggered,
		this,
		&myNotePad::outlineActionSlot
	);
	connect(
		ui.searchAction,
		&QAction::triggered,
		this,
		&myNotePad::searchActionSlot
	);
	connect(
		ui.soureCodeModeAction,
		&QAction::triggered,
		this,
		&myNotePad::soureCodeModeActionSlot
	);
	connect(
		ui.focusModeAction,
		&QAction::triggered,
		this,
		&myNotePad::focusModeActionSlot
	);
	connect(
		ui.typewriterModeAction,
		&QAction::triggered,
		this,
		&myNotePad::typewriterModeActionSlot
	);
	connect(
		ui.actualSizeAction,
		&QAction::triggered,
		this,
		&myNotePad::actualSizeActionSlot
	);
	connect(
		ui.zoomInAction,
		&QAction::triggered,
		this,
		&myNotePad::zoomInActionSlot
	);
	connect(
		ui.zoomOutAction,
		&QAction::triggered,
		this,
		&myNotePad::zoomOutActionSlot
	);
	//-------------------------------------------------------------
	connect(
		ui.githubAction,
		&QAction::triggered,
		this,
		&myNotePad::githubActionSlot
	);
	connect(
		ui.NewsprintAction,
		&QAction::triggered,
		this,
		&myNotePad::NewsprintActionSlot
	);
	connect(
		ui.NightAction,
		&QAction::triggered,
		this,
		&myNotePad::NightActionSlot
	);
	connect(
		ui.PixyllAction,
		&QAction::triggered,
		this,
		&myNotePad::PixyllActionSlot
	);
	connect(
		ui.WhiteyAction,
		&QAction::triggered,
		this,
		&myNotePad::WhiteyActionSlot
	);
	/*QPushButton* button = new QPushButton("test:markdown 转换按键", this);
	button->setStyleSheet("QPushButton{ color: red }");
	statusBar()->addWidget(button);*/
	
}

/*
1
2
3
4
5
6
加粗
斜体
下划线
删除线
测试
*/